<?php
if (!session_start()) session_start();
if (!isset($_SESSION['kiemtradangnhapthinguyet']) || $_SESSION['kiemtradangnhapthinguyet'] != 1) {
    header('location: ../../../index.php');
    die();
}
$pageSize = 20;
$pageNum  = 1;
if (isset($_GET['pageNum']) == true) $pageNum = $_GET['pageNum'];
$totalRows = 0;
$tukhoa    = '';
$idcl      = -1;
$idloai    = -1;
if (isset($_GET['tukhoa'])) {
    $tukhoa = $_GET['tukhoa'];
    $tukhoa = strtolower(trim($tukhoa));
    $idcl   = $_GET['idcl'];
    $idloai = $_GET['idloai'];

    $idloai = $idloai == 0 ? -1 : $idloai;
    $idcl   = $idcl == 0 ? -1 : $idcl;
}
$option = " and (idcl = $idcl or $idcl = -1)";
$option .= " and (idloai = $idloai or $idloai = -1)";
$res = $qt->sanpham_list2($pageNum, $pageSize, $totalRows, $tukhoa, $option);

$table = "sanpham";
?>
<script>
    $(document).ready(function () {
        $("img.newsp").click(function () {
            id = $(this).attr("idanhien");
            obj = this;
            $.ajax({
                url:'anhien.php',
                data:"table=<?=$table?>&loai=trangchu&id=" + id,
                cache:false,
                success:function (data) {
                    //alert(data);
                    obj.src = 'imgs/new_' + data + '.png';
                    if (data == "1.jpg") obj.title = "Nhắp vào để ẩn";
                    else obj.title = "Nhắp vào để hiện";
                }
            });
        });
        $("img.anhien").click(function () {
            id = $(this).attr("idanhien");
            obj = this;
            $.ajax({
                url:'anhien.php',
                data:"table=<?=$table?>&loai=anhien&id=" + id,
                cache:false,
                success:function (data) {
                    //alert(data);
                    obj.src = 'imgs/anhien_' + data + '.jpg';
                    if (data == "1.jpg") obj.title = "Nhắp vào để ẩn";
                    else obj.title = "Nhắp vào để hiện";
                }
            });
        });
        $("#chonhet").click(function () {
            var status = this.checked;
            $("input[name='chon']").each(function () {
                this.checked = status;
            })
        });

        $('#capnhatthutu').click(function () {
            var listid = "";
            $("input[name='listid']").each(function () {
                listid = listid + "," + this.value;
            });
            var listtt = "";
            $("input[name='thutu']").each(function () {
                listtt = listtt + "," + this.value;
            });
            listid = listid.substr(1);
            listtt = listtt.substr(1);
            if (listid == "") {
                alert("Bạn chưa chọn tin nào");
                return;
            }
            document.location = "capnhatthutu.php?table=<?=$table?>&listid=" + listid + "&listtt=" + listtt;
        });

        $("#xoahet").click(function () {
            var listid = "";
            $("input[name='chon']").each(function () {
                if (this.checked) listid = listid + "," + this.value;
            })
            listid = listid.substr(1);
            if (listid == "") {
                alert("You have not selected !");
                return;
            }
            hoi = confirm("Delete all ?");
            if (hoi == true)
                document.location = "do_delete_list_record.php?table=<?php echo $table; ?>&p=<?php echo $table; ?>_quanly&listid=" + listid;
        });
    });
</script>

<div class="dangnhap_title">
    <a href="index.php" style="color:#F8991D;" title="Trang chủ">Trang chủ</a>
    <span style="font-size:9px;">>></span>
    Sản phẩm
    <span style="font-size:9px;">>></span>
    <a href="index.php?p=<?php echo $table; ?>_quanly" style="color:#F8991D;">Sản phẩm</a>
</div>

<div style="float:right; width:710px; text-align:right;">
    <form method="get" action="index.php?p=<?php echo $table; ?>_quanly">
        <select id="idcl" name="idcl" style="width: 200px;" onchange="return getl();">
            <option value="0">--------Chọn danh mục--------</option>
            <?php

            $listChungLoai = $qt->get_by_order('sanpham_chungloai', -1, 1);

            while ($row = mysql_fetch_assoc($listChungLoai)) {
                ?>
                <option <?=$row['id'] == $idcl ? "selected" : ""  ?>
                        value="<?=$row['id']  ?>"><?=$row['ten_vi']  ?></option>
                <!-- <?php while ($row_loai = mysql_fetch_assoc($listLoai)) {
                    ?>
	                			<option value="<?= $row_loai['id'] ?>">---<?= $row_loai['ten_vi'] ?></option>
	                			<?php } ?> -->
                <?php
            } ?>
        </select>
        <?php if ($idcl == -1) { ?>
        <select id="idloai" name="idloai" style="width: 200px;">
            <option value="0">--------Chọn danh mục--------</option>
        </select>
        <?php
    } else {
        $listLoai = $qt->get_by_order('sanpham_loai', -1, 1, " and idcl = " . $idcl);
        ?>
        <select id="idloai" name="idloai" style="width: 200px;">
            <option value="0">--------Chọn danh mục--------</option>
            <?php while ($row_loai = mysql_fetch_assoc($listLoai)) {
            ?>
            <option <?=$row_loai['id'] == $idloai ? "selected" : ""  ?>
                    value="<?=$row_loai['id']  ?>"><?=$row_loai['ten_vi']  ?></option>
            <?php } ?>
        </select>
        <?php } ?>
        <b>Key words:</b> <input type="tukhoa" name="tukhoa"/>
        <input name="p" type="hidden" value="<?php echo $table; ?>_quanly"/>
        <input type="submit" value="Search">
    </form>
    <script type="text/javascript">
        function getl() {
            var id = document.getElementById("idcl").value;
            $("#idloai").load("librarys/incs/getloai.php?table=sanpham_loai&loai=idcl&id=" + id);
        }
    </script>
</div>

<div style="float:right; width:710px; position: relative;">
    <input title="Chọn rồi bấm xóa" type="button" name="xoahet" id="xoahet" value=" Xoá nhiều " class="nut"
           style="float:left; margin-top:24px;"/>

    <div class="phan_trang">
        <?php echo $qt->pagesList($totalRows, $pageNum, $pageSize); ?>
    </div>
    <img src="imgs/them.png" onclick="create_record('<?php echo $table; ?>_them');" width="48" height="48" border="0"
         style="cursor: pointer; float:right;"/>
</div>
<table border="0" cellpadding="0" cellspacing="0" style="float:right; width:710px; text-align:left;">
    <tr>
        <td width="40" class="td_style1"><input type="checkbox" name="chonhet" id="chonhet"/></td>
        <td width="34" class="td_style1"><b>Số</b></td>
        <td class="td_style1" align="center" width="168"><b>Hình</b></td>
        <td width="206" class="td_style1"><b>Tên</b></td>
        <td width="140" class="td_style1"><b>Parent</b></td>
        <td width="42" class="td_style1" style="padding-right:10px;"><b>Ẩn hiện</b></td>
        <td width="80" class="td_style1" align="center"><b>Công cụ</b></td>
    </tr>
    <? $dem = 0; while ($row = mysql_fetch_assoc($res)) {
    $dem++ ?>
    <tr>
        <td class="td_style2"><input type="checkbox" name="chon" id="chon" value="<?php echo $row['id']; ?>"
                                     class="chon"/></td>
        <td class="td_style2">#<?php echo $dem; ?></td>
        <td class="td_style2">
            <?php
            if ($row['url_img'] != "") {
                $images = explode(':*:', $row['url_img']);
                ?>
                <img src="<?php echo $images[0]; ?>" border="0" width="140" height="90"
                     style="margin-left:5px; margin-right:5px; border:solid 1px #999999;"/>
                <?php
            } else {
                ?>
                <img src="imgs/noimage.png" border="0" width="140" height="90"
                     style="margin-left:5px; margin-right:5px; border:solid 1px #999999;"/>
                <?php
            }
            ?>
        </td>
        <td class="td_style2">
            <b><?php echo $row['ten_vi']; ?></b> <br/>
            <?php echo date('d/m/Y H:i', strtotime($row['ngay'])); ?>
        </td>
        <td class="td_style2">
            <? $idcl = $row['idcl']; $sanpham_chungloai = mysql_fetch_assoc($qt->get_by_thutu('sanpham_chungloai', $idcl));?>
            <b>Chủng loại:</b> <?php echo $sanpham_chungloai['ten_vi']; ?>
            <br/>
            <? $idloai = $row['idloai']; $tin_loai = mysql_fetch_assoc($qt->get_by_thutu('sanpham_loai', $idloai));?>
            <b>Loại:</b> <?php echo $tin_loai['ten_vi']; ?>
        </td>
        <td class="td_style2" align="center" style="padding-right:10px;">
            <img alt="" idanhien="<?=$row['id']?>" src="imgs/anhien_<?=$row['anhien']?>.jpg" border="0"
                 style="cursor: pointer;" class="anhien"/>
            <br />
            <br />
            <img class="newsp" width="32" alt="" idanhien="<?=$row['id']?>" src="imgs/new_<?=$row['trangchu']?>.png" border="0" style="cursor: pointer;"  />
        </td>
        <td class="td_style2" align="center">
            <img src="imgs/pencil.png" onclick="update_record('<?php echo $table; ?>_sua','<?php echo $row['id']; ?>');"
                 width="16" height="16" border="0" style="cursor: pointer;"/>
            &nbsp;&nbsp;
            <img src="imgs/xoa.png"
                 onclick="if (confirm('Are you sure ?')) do_delete_record('<?php echo $table; ?>','<?php echo $row['id']; ?>');"
                 width="16" height="16" border="0" style="cursor: pointer;"/>
        </td>
    </tr>
    <?php
}
    ?>
</table>